package com.huawei.deveco.common.security.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Objects;

public final class FileUtil {
    private static final byte[] EMPTY_BYTE = new byte[0];

    private static final int KILOBYTE = 1024;

    private static final int MEGABYTE = 1048576;

    private static final int MAX_LOADING_SIZE = 10485760;

    public static byte[] readFileBytes(File file) throws IOException {
        Objects.requireNonNull(file, "file should not be null");
        if (!file.isFile())
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "%s is not a file!", new Object[]{file.getName()}));
        try {
            InputStream inputStream = new FileInputStream(file);
            try {
                byte[] arrayOfByte = readBytes(inputStream, (int) file.length());
                inputStream.close();
                return arrayOfByte;
            } catch (Throwable throwable) {
                try {
                    inputStream.close();
                } catch (Throwable throwable1) {
                    throwable.addSuppressed(throwable1);
                }
                throw throwable;
            }
        } catch (IllegalStateException e) {
            throw new IllegalStateException(String.format(Locale.ENGLISH, "file %s is too big!", new Object[]{file.getName()}));
        }
    }

    public static byte[] readBytes(InputStream inputStream, int length) throws IOException {
        Objects.requireNonNull(inputStream, "inputStream should not be null");
        if (length > 10485760)
            throw new IllegalStateException("length is too long!");
        if (length <= 0)
            return EMPTY_BYTE;
        byte[] bytes = new byte[length];
        int offset = 0;
        while (offset < length) {
            int count = inputStream.read(bytes, offset, length - offset);
            if (count <= 0)
                break;
            offset += count;
        }
        return bytes;
    }

    public static void writeBytes(File file, byte[] bytes) throws IOException {
        Objects.requireNonNull(file, "file should not be null");
        Objects.requireNonNull(bytes, "bytes should not be null");
        createParentDirs(file);
        OutputStream outputStream = new FileOutputStream(file);
        try {
            outputStream.write(bytes);
            outputStream.close();
        } catch (Throwable throwable) {
            try {
                outputStream.close();
            } catch (Throwable throwable1) {
                throwable.addSuppressed(throwable1);
            }
            throw throwable;
        }
    }

    private static void createParentDirs(File file) throws IOException {
        File parentDir = file.getParentFile();
        if (parentDir == null)
            throw new IOException("parent file is null");
        if (!parentDir.exists()) {
            if (parentDir.mkdirs())
                return;
            throw new IOException("create files failed!");
        }
        if (!parentDir.isDirectory())
            throw new IOException("Invalid path!");
    }
}
